<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>QSS (Qt Style Sheet) 样式设定 | Xing's Blog</title><meta name="author" content="xing"><meta name="copyright" content="xing"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="QSS (Qt Style Sheet) 样式设定">
<meta property="og:type" content="article">
<meta property="og:title" content="QSS (Qt Style Sheet) 样式设定">
<meta property="og:url" content="https://veryxing.github.io/article/bf8fd827.html">
<meta property="og:site_name" content="Xing&#39;s Blog">
<meta property="og:description" content="QSS (Qt Style Sheet) 样式设定">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-6.png">
<meta property="article:published_time" content="2024-10-14T07:20:13.000Z">
<meta property="article:modified_time" content="2024-10-31T15:25:09.209Z">
<meta property="article:author" content="xing">
<meta property="article:tag" content="PyQt">
<meta property="article:tag" content="Qss">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-6.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://veryxing.github.io/article/bf8fd827.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!true && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        if (name && globalFn[key][name]) return
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.json","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"未找到符合您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":true},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'QSS (Qt Style Sheet) 样式设定',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-10-31 23:25:09'
}</script><meta name="generator" content="Hexo 7.3.0"><link rel="alternate" href="/atom.xml" title="Xing's Blog" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "/images/2.gif" data-lazy-src="/img/butterfly-icon.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">59</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">55</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/gallery/"><i class="fa-fw fas fa-folder-open"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/shuoshuo/"><i class="fa-fw fas fa-folder-open"></i><span> 说说</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-6.png);"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">Xing's Blog</span></a><a class="nav-page-title" href="/"><span class="site-name">QSS (Qt Style Sheet) 样式设定</span></a></span><div id="menus"><div id="search-button"><span class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></span></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/gallery/"><i class="fa-fw fas fa-folder-open"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/shuoshuo/"><i class="fa-fw fas fa-folder-open"></i><span> 说说</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">QSS (Qt Style Sheet) 样式设定</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-10-14T07:20:13.000Z" title="发表于 2024-10-14 15:20:13">2024-10-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-10-31T15:25:09.209Z" title="更新于 2024-10-31 23:25:09">2024-10-31</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/PyQt/">PyQt</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>QSS 是 PyQt6 裡用來設定元件樣式的樣式表 ( Qt Style Sheet )，使用方法和網頁 CSS 非常類似，雖然 QSS 沒有辦法像網頁 CSS 般的完整，但已經可以滿足大部分的樣式設計需求，這篇教學會介紹如何使用 QSS 設定元件樣式。<span id="more"></span></p>
<h2 id="開始使用-QSS"><a href="#開始使用-QSS" class="headerlink" title="開始使用 QSS"></a>開始使用 QSS</h2><p>在 PyQt6 裡建立元件之後，元件會具有一個 setStyleSheet() 方法，在方法裡撰寫樣式，就會修改原本元件的樣式，例如下方的程式碼執行後，就會將 QLabel 的文字設定為 30px 的大小和紅色 ( 如果遇到多個樣式，可以使用三個單引號的方式撰寫 )。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PyQt6 <span class="keyword">import</span> QtWidgets</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line">app = QtWidgets.QApplication(sys.argv)</span><br><span class="line"></span><br><span class="line">Form = QtWidgets.QWidget()</span><br><span class="line">Form.setWindowTitle(<span class="string">&#x27;oxxo.studio&#x27;</span>)</span><br><span class="line">Form.resize(<span class="number">300</span>, <span class="number">200</span>)</span><br><span class="line"></span><br><span class="line">label = QtWidgets.QLabel(Form)</span><br><span class="line">label.setGeometry(<span class="number">10</span>,<span class="number">10</span>,<span class="number">150</span>,<span class="number">100</span>)</span><br><span class="line">label.setText(<span class="string">&#x27;HELLO&#x27;</span>)</span><br><span class="line">label.setStyleSheet(<span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">        font-size:30px;</span></span><br><span class="line"><span class="string">        color:red;</span></span><br><span class="line"><span class="string">    &#x27;&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line">Form.show()</span><br><span class="line">sys.exit(app.<span class="built_in">exec</span>())</span><br></pre></td></tr></table></figure>

<p>class 寫法：</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PyQt6 <span class="keyword">import</span> QtWidgets</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyWidget</span>(QtWidgets.QWidget):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>().__init__()</span><br><span class="line">        <span class="variable language_">self</span>.setObjectName(<span class="string">&quot;MainWindow&quot;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.setWindowTitle(<span class="string">&#x27;oxxo.studio&#x27;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.resize(<span class="number">300</span>, <span class="number">200</span>)</span><br><span class="line">        <span class="variable language_">self</span>.ui()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">ui</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="variable language_">self</span>.label = QtWidgets.QLabel(<span class="variable language_">self</span>)</span><br><span class="line">        <span class="variable language_">self</span>.label.setGeometry(<span class="number">10</span>,<span class="number">10</span>,<span class="number">150</span>,<span class="number">100</span>)</span><br><span class="line">        <span class="variable language_">self</span>.label.setText(<span class="string">&#x27;HELLO&#x27;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.label.setStyleSheet(<span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">                font-size:30px;</span></span><br><span class="line"><span class="string">                color:red;</span></span><br><span class="line"><span class="string">            &#x27;&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    app = QtWidgets.QApplication(sys.argv)</span><br><span class="line">    Form = MyWidget()</span><br><span class="line">    Form.show()</span><br><span class="line">    sys.exit(app.<span class="built_in">exec</span>())</span><br></pre></td></tr></table></figure>

<p><img src= "/images/2.gif" data-lazy-src="/assets/qss-01-1728890756438-3.webp" alt="PyQt6 教學 - QSS ( Qt Style Sheet ) 樣式設定"></p>
<h2 id="常用樣式設定"><a href="#常用樣式設定" class="headerlink" title="常用樣式設定"></a>常用樣式設定</h2><p>下方列出一些常用的樣式 ( 完整屬性參考「<a target="_blank" rel="noopener" href="https://doc.qt.io/qt-6/stylesheet-reference.html#list-of-properties">List of Properties</a>」)。</p>
<table>
<thead>
<tr>
<th>樣式</th>
<th>說明</th>
</tr>
</thead>
<tbody><tr>
<td>font-size</td>
<td>文字大小，單位 px。</td>
</tr>
<tr>
<td>color</td>
<td>文字顏色，可使用顏色名稱或色碼，例如 #f00 為紅色。</td>
</tr>
<tr>
<td>font-family</td>
<td>字體。</td>
</tr>
<tr>
<td>font-weight</td>
<td>字體粗細，可設定 normal、bold。</td>
</tr>
<tr>
<td>font-style</td>
<td>文字樣式，可設定 normal、italic、oblique。</td>
</tr>
<tr>
<td>spacing</td>
<td>文字間距，不用單位。</td>
</tr>
<tr>
<td>text-align</td>
<td>文字對齊方式，可設定 left、center、right。</td>
</tr>
<tr>
<td>height</td>
<td>元件高度，單位 px。</td>
</tr>
<tr>
<td>width</td>
<td>元件寬度，單位 px。</td>
</tr>
<tr>
<td>margin</td>
<td>元件外邊距，單位 px。</td>
</tr>
<tr>
<td>padding</td>
<td>元件內邊距，單位 px。</td>
</tr>
<tr>
<td>opacity</td>
<td>透明度，範圍 0～255，0 為透明。</td>
</tr>
<tr>
<td>background</td>
<td>背景色或背景圖，可使用顏色名稱或色碼，例如 #f00 為紅色。</td>
</tr>
<tr>
<td>background-color</td>
<td>背景色，可使用顏色名稱或色碼，例如 #f00 為紅色。</td>
</tr>
<tr>
<td>border</td>
<td>邊框，有三個值分別是 ( 粗細、樣式、顏色 )。</td>
</tr>
<tr>
<td>border-width</td>
<td>邊框寬度，單位 px。</td>
</tr>
<tr>
<td>border-style</td>
<td>邊框樣式，可設定 solid、dashed、dotted。</td>
</tr>
<tr>
<td>border-color</td>
<td>邊框顏色，可使用顏色名稱或色碼，例如 #f00 為紅色。</td>
</tr>
<tr>
<td>border-radius</td>
<td>邊框是否圓角，圓角半徑單位 px。</td>
</tr>
</tbody></table>
<p>樣式裡的 padding、margin、border-width、border-style、border-color 的簡單設定規則如下 ( 以 padding 為例 )：</p>
<table>
<thead>
<tr>
<th>寫法</th>
<th>說明</th>
</tr>
</thead>
<tbody><tr>
<td>padding:1px</td>
<td>上下左右都 1px。</td>
</tr>
<tr>
<td>padding:1px 2px</td>
<td>上下 1px，左右 2px。</td>
</tr>
<tr>
<td>padding:1px 2px 3px</td>
<td>上 1px，左右 2px，下 3px。</td>
</tr>
<tr>
<td>padding:1px 2px 3px 4px</td>
<td>上 1px，右 2px，下 3px，左 4px。</td>
</tr>
</tbody></table>
<p>如果不要一次設定 padding、margin、border、border-width、border-style、border-color，也可以加上方向的名稱單獨設定 ( 以 padding 為例 )：</p>
<table>
<thead>
<tr>
<th>樣式</th>
<th>說明</th>
</tr>
</thead>
<tbody><tr>
<td>padding-top</td>
<td>元件上方內邊距，單位 px。</td>
</tr>
<tr>
<td>padding-right</td>
<td>元件右側內邊距，單位 px。</td>
</tr>
<tr>
<td>padding-bottom</td>
<td>元件下方內邊距，單位 px。</td>
</tr>
<tr>
<td>padding-left</td>
<td>元件左側內邊距，單位 px。</td>
</tr>
</tbody></table>
<h2 id="偽狀態設定"><a href="#偽狀態設定" class="headerlink" title="偽狀態設定"></a>偽狀態設定</h2><p>除了設定主要樣式，QSS 也像 CSS 一樣可以設定「偽狀態」( Pseudo-States )，偽類的意思是「觸發了某些事件或進行某些行為後，才會出現的狀態」 ( 偽類會使用一個冒號開頭 )，例如「滑鼠移到按鈕上」的行為，對應的偽狀態就是「:hover」，下方列出一些常見的偽狀態 ( 完整偽狀態參考「<a target="_blank" rel="noopener" href="https://doc.qt.io/qt-6/stylesheet-reference.html#list-of-pseudo-states">List of Pseudo-States</a>」 )：</p>
<table>
<thead>
<tr>
<th>偽狀態</th>
<th>說明</th>
</tr>
</thead>
<tbody><tr>
<td>:hover</td>
<td>滑鼠移上去。</td>
</tr>
<tr>
<td>:active</td>
<td>發生行為 ( 通常可能是點擊 )。</td>
</tr>
<tr>
<td>:focus</td>
<td>成為焦點 ( 通常是點擊之後 )。</td>
</tr>
<tr>
<td>:checked</td>
<td>被勾選。</td>
</tr>
<tr>
<td>:disabled</td>
<td>停用狀態。</td>
</tr>
<tr>
<td>:enabled</td>
<td>啟用狀態。</td>
</tr>
<tr>
<td>:selected</td>
<td>被選取。</td>
</tr>
</tbody></table>
<p>以下方的程式碼為例，執行後會設定 QPushButton 的 :hover 樣式 ( 注意需要額外使用 QPushButton:hover )，當滑鼠移動到按鈕上方時，按鈕的邊框會變粗，背景色會變成黃色。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PyQt6 <span class="keyword">import</span> QtWidgets</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line">app = QtWidgets.QApplication(sys.argv)</span><br><span class="line"></span><br><span class="line">Form = QtWidgets.QWidget()</span><br><span class="line">Form.setWindowTitle(<span class="string">&#x27;oxxo.studio&#x27;</span>)</span><br><span class="line">Form.resize(<span class="number">300</span>, <span class="number">200</span>)</span><br><span class="line"></span><br><span class="line">btn = QtWidgets.QPushButton(Form)</span><br><span class="line">btn.setGeometry(<span class="number">10</span>,<span class="number">10</span>,<span class="number">150</span>,<span class="number">100</span>)</span><br><span class="line">btn.setText(<span class="string">&#x27;HELLO&#x27;</span>)</span><br><span class="line">btn.setStyleSheet(<span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">    QPushButton&#123;</span></span><br><span class="line"><span class="string">        border:1px solid #000;</span></span><br><span class="line"><span class="string">        background:#fff;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    QPushButton:hover&#123;</span></span><br><span class="line"><span class="string">        border:5px solid #000;</span></span><br><span class="line"><span class="string">        background:#ff0;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line">Form.show()</span><br><span class="line">sys.exit(app.<span class="built_in">exec</span>())</span><br></pre></td></tr></table></figure>

<p>class 寫法：</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PyQt6 <span class="keyword">import</span> QtWidgets</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyWidget</span>(QtWidgets.QWidget):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>().__init__()</span><br><span class="line">        <span class="variable language_">self</span>.setObjectName(<span class="string">&quot;MainWindow&quot;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.setWindowTitle(<span class="string">&#x27;oxxo.studio&#x27;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.resize(<span class="number">300</span>, <span class="number">200</span>)</span><br><span class="line">        <span class="variable language_">self</span>.ui()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">ui</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="variable language_">self</span>.btn = QtWidgets.QPushButton(<span class="variable language_">self</span>)</span><br><span class="line">        <span class="variable language_">self</span>.btn.setGeometry(<span class="number">10</span>,<span class="number">10</span>,<span class="number">150</span>,<span class="number">100</span>)</span><br><span class="line">        <span class="variable language_">self</span>.btn.setText(<span class="string">&#x27;HELLO&#x27;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.btn.setStyleSheet(<span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">            QPushButton&#123;</span></span><br><span class="line"><span class="string">                border:1px solid #000;</span></span><br><span class="line"><span class="string">                background:#fff;</span></span><br><span class="line"><span class="string">            &#125;</span></span><br><span class="line"><span class="string">            QPushButton:hover&#123;</span></span><br><span class="line"><span class="string">                border:5px solid #000;</span></span><br><span class="line"><span class="string">                background:#ff0;</span></span><br><span class="line"><span class="string">            &#125;</span></span><br><span class="line"><span class="string">        &#x27;&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    app = QtWidgets.QApplication(sys.argv)</span><br><span class="line">    Form = MyWidget()</span><br><span class="line">    Form.show()</span><br><span class="line">    sys.exit(app.<span class="built_in">exec</span>())</span><br></pre></td></tr></table></figure>

<p><img src= "/images/2.gif" data-lazy-src="/assets/qss-02.webp" alt="PyQt6 教學 - QSS ( Qt Style Sheet ) 樣式設定 - 偽狀態設定"></p>
<h2 id="子控制項設定"><a href="#子控制項設定" class="headerlink" title="子控制項設定"></a>子控制項設定</h2><p>在 PyQt6 裡，有些元件可能會包含其他的子控制項 ( 例如 QListWidget 列表選擇框會包含選項的子元件 )，這些子元件也可以使用對應的 QSS 語法設定樣式 ( 子控制項會使用兩個冒號開頭 )，下方列出一些常見的子元件設定方法 ( 完整子元件設定參考「<a target="_blank" rel="noopener" href="https://doc.qt.io/qt-6/stylesheet-reference.html#list-of-sub-controls">List of Sub-Controls</a>」 )：</p>
<table>
<thead>
<tr>
<th>子控制項</th>
<th>說明</th>
</tr>
</thead>
<tbody><tr>
<td>::chunk</td>
<td>進度條進度。</td>
</tr>
<tr>
<td>::item</td>
<td>列表選擇框項目。</td>
</tr>
<tr>
<td>::groove</td>
<td>滑桿底線。</td>
</tr>
<tr>
<td>::handle</td>
<td>滑桿拉霸。</td>
</tr>
<tr>
<td>::sub-page</td>
<td>滑桿調整線。</td>
</tr>
</tbody></table>
<p>下方的程式碼執行後，會將 QSlider 數值調整滑桿更改為黑底線與紅色調整桿。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PyQt6 <span class="keyword">import</span> QtWidgets, QtCore</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line">app = QtWidgets.QApplication(sys.argv)</span><br><span class="line"></span><br><span class="line">Form = QtWidgets.QWidget()</span><br><span class="line">Form.setWindowTitle(<span class="string">&#x27;oxxo.studio&#x27;</span>)</span><br><span class="line">Form.resize(<span class="number">300</span>, <span class="number">200</span>)</span><br><span class="line"></span><br><span class="line">slider = QtWidgets.QSlider(Form)</span><br><span class="line">slider.setGeometry(<span class="number">20</span>,<span class="number">20</span>,<span class="number">200</span>,<span class="number">30</span>)</span><br><span class="line">slider.setOrientation(QtCore.Qt.Orientation.Horizontal)</span><br><span class="line">slider.setStyleSheet(<span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">    QSlider &#123;</span></span><br><span class="line"><span class="string">        border-radius: 10px;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    QSlider::groove:horizontal &#123;</span></span><br><span class="line"><span class="string">        height: 5px;</span></span><br><span class="line"><span class="string">        background: #000;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    QSlider::handle:horizontal&#123;</span></span><br><span class="line"><span class="string">        background: #f00;</span></span><br><span class="line"><span class="string">        width: 16px;</span></span><br><span class="line"><span class="string">        height: 16px;</span></span><br><span class="line"><span class="string">        margin:-6px 0;</span></span><br><span class="line"><span class="string">        border-radius:8px;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    QSlider::sub-page:horizontal&#123;</span></span><br><span class="line"><span class="string">        background:#f90;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line">Form.show()</span><br><span class="line">sys.exit(app.<span class="built_in">exec</span>())</span><br></pre></td></tr></table></figure>

<p>class 寫法：</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PyQt6 <span class="keyword">import</span> QtWidgets, QtCore</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyWidget</span>(QtWidgets.QWidget):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>().__init__()</span><br><span class="line">        <span class="variable language_">self</span>.setObjectName(<span class="string">&quot;MainWindow&quot;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.setWindowTitle(<span class="string">&#x27;oxxo.studio&#x27;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.resize(<span class="number">300</span>, <span class="number">200</span>)</span><br><span class="line">        <span class="variable language_">self</span>.ui()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">ui</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="variable language_">self</span>.slider = QtWidgets.QSlider(<span class="variable language_">self</span>)</span><br><span class="line">        <span class="variable language_">self</span>.slider.setGeometry(<span class="number">20</span>,<span class="number">20</span>,<span class="number">200</span>,<span class="number">30</span>)</span><br><span class="line">        <span class="variable language_">self</span>.slider.setOrientation(QtCore.Qt.Orientation.Horizontal)</span><br><span class="line">        <span class="variable language_">self</span>.slider.setStyleSheet(<span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">            QSlider &#123;</span></span><br><span class="line"><span class="string">                border-radius: 10px;</span></span><br><span class="line"><span class="string">            &#125;</span></span><br><span class="line"><span class="string">            QSlider::groove:horizontal &#123;</span></span><br><span class="line"><span class="string">                height: 5px;</span></span><br><span class="line"><span class="string">                background: #000;</span></span><br><span class="line"><span class="string">            &#125;</span></span><br><span class="line"><span class="string">            QSlider::handle:horizontal&#123;</span></span><br><span class="line"><span class="string">                background: #f00;</span></span><br><span class="line"><span class="string">                width: 16px;</span></span><br><span class="line"><span class="string">                height: 16px;</span></span><br><span class="line"><span class="string">                margin:-6px 0;</span></span><br><span class="line"><span class="string">                border-radius:8px;</span></span><br><span class="line"><span class="string">            &#125;</span></span><br><span class="line"><span class="string">            QSlider::sub-page:horizontal&#123;</span></span><br><span class="line"><span class="string">                background:#f90;</span></span><br><span class="line"><span class="string">            &#125;</span></span><br><span class="line"><span class="string">        &#x27;&#x27;&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    app = QtWidgets.QApplication(sys.argv)</span><br><span class="line">    Form = MyWidget()</span><br><span class="line">    Form.show()</span><br><span class="line">    sys.exit(app.<span class="built_in">exec</span>())</span><br></pre></td></tr></table></figure>

<p><img src= "/images/2.gif" data-lazy-src="/assets/qss-03.webp" alt="PyQt6 教學 - QSS ( Qt Style Sheet ) 樣式設定 - 子控制項設定"></p>
<h2 id="注意事項"><a href="#注意事項" class="headerlink" title="注意事項"></a>注意事項</h2><p>雖然使用 QSS 可以很方便的修改樣式，但仍然有些小細節需要注意，例如 QPushButton，一旦設定了「邊框」，則必須要一併設定背景色和點擊樣式，不然其他樣式就會被清空，類似這種狀況可以參考「<a target="_blank" rel="noopener" href="https://doc.qt.io/qt-6/stylesheet-reference.html">Qt Style Sheets Reference</a>」裡有特別標注「粉紅色背景」的說明。</p>
<p>下方的程式碼執行後，會發現本來按鈕的點擊效果，因為設定了 border 樣式而消失了。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PyQt6 <span class="keyword">import</span> QtWidgets</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line">app = QtWidgets.QApplication(sys.argv)</span><br><span class="line"></span><br><span class="line">Form = QtWidgets.QWidget()</span><br><span class="line">Form.setWindowTitle(<span class="string">&#x27;oxxo.studio&#x27;</span>)</span><br><span class="line">Form.resize(<span class="number">300</span>, <span class="number">200</span>)</span><br><span class="line"></span><br><span class="line">btn = QtWidgets.QPushButton(Form)</span><br><span class="line">btn.setGeometry(<span class="number">10</span>,<span class="number">10</span>,<span class="number">150</span>,<span class="number">100</span>)</span><br><span class="line">btn.setText(<span class="string">&#x27;HELLO&#x27;</span>)</span><br><span class="line">btn.setStyleSheet(<span class="string">&#x27;border:1px solid #000&#x27;</span>)</span><br><span class="line"></span><br><span class="line">Form.show()</span><br><span class="line">sys.exit(app.<span class="built_in">exec</span>())</span><br></pre></td></tr></table></figure>

<p>class 寫法：</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PyQt6 <span class="keyword">import</span> QtWidgets, QtCore</span><br><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">MyWidget</span>(QtWidgets.QWidget):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>().__init__()</span><br><span class="line">        <span class="variable language_">self</span>.setObjectName(<span class="string">&quot;MainWindow&quot;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.setWindowTitle(<span class="string">&#x27;oxxo.studio&#x27;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.resize(<span class="number">300</span>, <span class="number">200</span>)</span><br><span class="line">        <span class="variable language_">self</span>.ui()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">ui</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="variable language_">self</span>.btn = QtWidgets.QPushButton(<span class="variable language_">self</span>)</span><br><span class="line">        <span class="variable language_">self</span>.btn.setGeometry(<span class="number">10</span>,<span class="number">10</span>,<span class="number">150</span>,<span class="number">100</span>)</span><br><span class="line">        <span class="variable language_">self</span>.btn.setText(<span class="string">&#x27;HELLO&#x27;</span>)</span><br><span class="line">        <span class="variable language_">self</span>.btn.setStyleSheet(<span class="string">&#x27;border:1px solid #000&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    app = QtWidgets.QApplication(sys.argv)</span><br><span class="line">    Form = MyWidget()</span><br><span class="line">    Form.show()</span><br><span class="line">    sys.exit(app.<span class="built_in">exec</span>())</span><br></pre></td></tr></table></figure>

<p><img src= "/images/2.gif" data-lazy-src="/assets/qss-04.webp" alt="PyQt6 教學 - QSS ( Qt Style Sheet ) 樣式設定 - 注意事項"></p>
<h2 id="參考資料"><a href="#參考資料" class="headerlink" title="參考資料"></a>參考資料</h2><p>完整 QSS 說明可以參考：<a target="_blank" rel="noopener" href="https://doc.qt.io/qt-6/stylesheet-reference.html">Qt Style Sheets Reference</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://veryXing.github.io">xing</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://veryxing.github.io/article/bf8fd827.html">https://veryxing.github.io/article/bf8fd827.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://veryXing.github.io" target="_blank">Xing's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/PyQt/">PyQt</a><a class="post-meta__tags" href="/tags/Qss/">Qss</a></div><div class="post-share"><div class="social-share" data-image="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-6.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><a class="prev-post pull-left" href="/article/a0a0fd82.html" title="PyQt5实现桌面小宠物"><img class="cover" src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">PyQt5实现桌面小宠物</div></div></a><a class="next-post pull-right" href="/article/0.html" title="pyqt5 无边框界面"><img class="cover" src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">pyqt5 无边框界面</div></div></a></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a href="/article/2241662d.html" title="PyQt5窗口跟随其它窗口"><img class="cover" src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-3.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2018-10-23</div><div class="title">PyQt5窗口跟随其它窗口</div></div></a><a href="/article/32b656da.html" title="PyQt密码管理系统"><img class="cover" src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-2.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2024-10-20</div><div class="title">PyQt密码管理系统</div></div></a><a href="/article/f305b1cc.html" title="PyQt属性动画(QPropertyAnimation)"><img class="cover" src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-4.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-05-08</div><div class="title">PyQt属性动画(QPropertyAnimation)</div></div></a><a href="/article/5a4e93df.html" title="PyQt5动画边框阴影"><img class="cover" src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-6.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2018-09-25</div><div class="title">PyQt5动画边框阴影</div></div></a><a href="/article/3217c859.html" title="三种方式绑定信号槽"><img class="cover" src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-10.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-05-04</div><div class="title">三种方式绑定信号槽</div></div></a><a href="/article/729610ce.html" title="PyQt5圆形图片"><img class="cover" src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-1.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2018-09-25</div><div class="title">PyQt5圆形图片</div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info is-center"><div class="avatar-img"><img src= "/images/2.gif" data-lazy-src="/img/butterfly-icon.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">xing</div><div class="author-info-description"></div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">59</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">55</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">9</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/veryXing"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%96%8B%E5%A7%8B%E4%BD%BF%E7%94%A8-QSS"><span class="toc-number">1.</span> <span class="toc-text">開始使用 QSS</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%A8%A3%E5%BC%8F%E8%A8%AD%E5%AE%9A"><span class="toc-number">2.</span> <span class="toc-text">常用樣式設定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%81%BD%E7%8B%80%E6%85%8B%E8%A8%AD%E5%AE%9A"><span class="toc-number">3.</span> <span class="toc-text">偽狀態設定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%90%E6%8E%A7%E5%88%B6%E9%A0%85%E8%A8%AD%E5%AE%9A"><span class="toc-number">4.</span> <span class="toc-text">子控制項設定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A0%85"><span class="toc-number">5.</span> <span class="toc-text">注意事項</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%83%E8%80%83%E8%B3%87%E6%96%99"><span class="toc-number">6.</span> <span class="toc-text">參考資料</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/article/18647ffa.html" title="pytest-qt 测试模态窗体."><img src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="pytest-qt 测试模态窗体."/></a><div class="content"><a class="title" href="/article/18647ffa.html" title="pytest-qt 测试模态窗体.">pytest-qt 测试模态窗体.</a><time datetime="2024-11-27T17:01:35.284Z" title="发表于 2024-11-28 01:01:35">2024-11-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/5a8a6c8d.html" title="post"><img src= "/images/2.gif" data-lazy-src="http://up.36992.com/pic/e6/c5/51/e6c551e768092c8655292d89a4034a74.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="post"/></a><div class="content"><a class="title" href="/article/5a8a6c8d.html" title="post">post</a><time datetime="2024-10-31T15:27:17.000Z" title="发表于 2024-10-31 23:27:17">2024-10-31</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/140ab620.html" title="某网站自动下载音乐mp3和歌词"><img src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-6.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="某网站自动下载音乐mp3和歌词"/></a><div class="content"><a class="title" href="/article/140ab620.html" title="某网站自动下载音乐mp3和歌词">某网站自动下载音乐mp3和歌词</a><time datetime="2024-10-31T12:15:16.000Z" title="发表于 2024-10-31 20:15:16">2024-10-31</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/32b656da.html" title="PyQt密码管理系统"><img src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-2.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="PyQt密码管理系统"/></a><div class="content"><a class="title" href="/article/32b656da.html" title="PyQt密码管理系统">PyQt密码管理系统</a><time datetime="2024-10-20T14:36:18.000Z" title="发表于 2024-10-20 22:36:18">2024-10-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/a0a0fd82.html" title="PyQt5实现桌面小宠物"><img src= "/images/2.gif" data-lazy-src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="PyQt5实现桌面小宠物"/></a><div class="content"><a class="title" href="/article/a0a0fd82.html" title="PyQt5实现桌面小宠物">PyQt5实现桌面小宠物</a><time datetime="2024-10-20T14:28:05.000Z" title="发表于 2024-10-20 22:28:05">2024-10-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2024 By xing</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="true" data-click="false"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>(() => {
  const pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

  window.pjax = new Pjax({
    elements: 'a:not([target="_blank"])',
    selectors: pjaxSelectors,
    cacheBust: false,
    analytics: false,
    scrollRestoration: false
  })

  const triggerPjaxFn = (val) => {
    if (!val) return
    Object.values(val).forEach(fn => fn())
  }

  document.addEventListener('pjax:send', () => {
    // removeEventListener
    btf.removeGlobalFnEvent('pjaxSendOnce')
    btf.removeGlobalFnEvent('themeChange')

    // reset readmode
    const $bodyClassList = document.body.classList
    if ($bodyClassList.contains('read-mode')) $bodyClassList.remove('read-mode')

    triggerPjaxFn(window.globalFn.pjaxSend)
  })

  document.addEventListener('pjax:complete', () => {
    btf.removeGlobalFnEvent('pjaxCompleteOnce')
    document.querySelectorAll('script[data-pjax]').forEach(item => {
      const newScript = document.createElement('script')
      const content = item.text || item.textContent || item.innerHTML || ""
      Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
      newScript.appendChild(document.createTextNode(content))
      item.parentNode.replaceChild(newScript, item)
    })

    triggerPjaxFn(window.globalFn.pjaxComplete)
  })

  document.addEventListener('pjax:error', e => {
    if (e.request.status === 404) {
      pjax.loadUrl('/404.html')
    }
  })
})()</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>